package com.example.hellomap;

import android.app.Activity;
import android.location.LocationListener;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

/**
 * @author Alle
 * @param args
 */
public class MyGoogleMap implements OnMapClickListener, OnMapLongClickListener,
		OnMarkerDragListener {

	private GoogleMap myGoogleMap;
	private MyLocationListener myLocListener;
	private final LatLng budapest = new LatLng(47.505142, 19.041939);	
	boolean markerClicked;	
	
	public GoogleMap getMyGoogleMap() {
		return myGoogleMap;
	}

	public MyLocationListener getMyLocListener() {
		return myLocListener;
	}

	public MyGoogleMap(Activity activity, MyLocationListener locationListener) {
		
		myLocListener = locationListener;
		//textViewLocationInfo = (TextView)parentActivity.findViewById(R.id.locinfo);	
	}
	
	void setUpMapIfNeeded(GoogleMap gmap) {
		
		if (myGoogleMap != null) {
			return;
		}
		myGoogleMap = gmap;
		if (myGoogleMap == null) {
			return;
		}
	}
	
	void initMap() {
		
		myGoogleMap.setMyLocationEnabled(true);		
		myGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);		
		
		// Map listenerek:
		myGoogleMap.setOnMapClickListener(this);
		myGoogleMap.setOnMapLongClickListener(this);
		myGoogleMap.setOnMarkerDragListener(this);

		markerClicked = false;		
		
		// Kezdőkoordináták
			
		myGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(budapest, 10));		

		// Jelölő létrehozása a kezdő koordinátára
		myGoogleMap.addMarker(new MarkerOptions()
				.icon(BitmapDescriptorFactory.fromResource(R.drawable.home)) // Saját ikon a jelölőre
					// .anchor(0.0f, 1.0f) // Anchors the marker on the bott
				.title("Budapest")
				.snippet("Kezdőkoordinátád").position(budapest));
		myGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
							new LatLng( myLocListener.getLat(),
										myLocListener.getLng()),
							12));		
		
	}


	@Override
	public void onMapClick(LatLng point) {
		//textViewLocationInfo.setText(point.toString());
		myGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(point));
		markerClicked = false;
	}

	@Override
	public void onMapLongClick(LatLng point) {
		//textViewLocationInfo.setText("New marker added@" + point.toString());
		BitmapDescriptor bitmapDsc = BitmapDescriptorFactory
				.defaultMarker(BitmapDescriptorFactory.HUE_AZURE);
		myGoogleMap.addMarker(new MarkerOptions().position(point).icon(bitmapDsc)
				.draggable(true).title("Helyzet" + point.toString()));
		markerClicked = false;		
	}

	@Override
	public void onMarkerDrag(Marker marker) {
		//textViewLocationInfo.setText("Marker " + marker.getId() + " Drag@"
				//+ marker.getPosition());
	}
    @Override
    public void onMarkerDragEnd(Marker marker) {
      //  textViewLocationInfo.setText("Marker " + marker.getId() + " DragEnd");
    }

    @Override
    public void onMarkerDragStart(Marker marker) {
       // textViewLocationInfo.setText("Marker " + marker.getId() + " DragStart");

    }



}
